export const inject_js_template = `
const allViews = import.meta.glob('@/views/**/*.tsx')
  
    const regex = /\\/src\\/views\\/([^/]+(?:\\/[^/]+)*)\\.tsx$/
    const paths = Object.keys(allViews)
      .filter((item) => !item.includes('/components/'))
      .map((item) => {
        const match = item.match(regex)
        if (match) {
          return match[1]
        }
        return null
      })
      .filter((item) => item != null)

    const createRoute = (path, config) => {
      // 取出路径中最后面的index，login/index => login
      let _path = path.replace(/\\/index$/, '')

      // 替换路由中的 __xx 为 :xx ，detail/__id => detail/:id
      _path = _path.replace(/\\__(.*?)/g, ':$1')

      // 取出路径中最后 / 的内容， test/blog.w.o.layout => test/blog/w/o/layout
      const arr = _path.split('/')
      if (arr.length > 1) {
        const lastPath = arr.pop().replace(/\\./g, '/')
        _path = arr.join('/') + '/' + lastPath
      }
      
      return {
        path: "/"+_path,
        lazy: () => allViews['/src/views/' + path + '.tsx']().then(module => ({ Component: module.default })),
        ...config,
      }
    }

    export const getRoutes= ()=>{
      const routes = paths.map((path) => createRoute(path));
      return routes;
    }
`

export const loading_template = `
import { } from 'react';

export default function Loading() {
    return (
        <div className="loading">
            <span>Loading...</span>
        </div>
    );
}
`

export const auth_route_tsx_template = `
import { FC, ReactNode } from 'react'
import { useLocation, Navigate } from 'react-router-dom';

interface AuthRouteProp {
  children: ReactNode | ReactNode[]
}

export const AuthRoute: FC<AuthRouteProp> = ({ children }) => {
  const location=useLocation();
  const isAuthenticated = true; // 假设这是鉴权的结果，实际中可能从存储或其他方式获取

  if (!isAuthenticated && location.pathname !== '/login') {
    return <Navigate to="/login" state={{ from: location }} replace />;
  }

  return children;
}

export default AuthRoute;
`

export const auth_route_jsx_template = `
import { FC, ReactNode } from 'react'
import { useLocation, Navigate } from 'react-router-dom';

export const AuthRoute: FC<AuthRouteProp> = ({ children }) => {
  const location=useLocation();
  const isAuthenticated = true; // 假设这是鉴权的结果，实际中可能从存储或其他方式获取

  if (!isAuthenticated && location.pathname !== '/login') {
    return <Navigate to="/login" state={{ from: location }} replace />;
  }

  return children;
}

export default AuthRoute;
`

export const routes_tsx_template = `
import { RouteObject } from 'react-router-dom'
import { Suspense, lazy } from 'react';
import AuthRoute from './AuthRoute';
import Loading from './Loading';

{noLazyImport}

{lazyImport}

type CustomRouteObject = RouteObject & {

}


export const routes=[
{routes}
] as CustomRouteObject[];


export default routes;
`

export const routes_jsx_template = `
import { RouteObject } from 'react-router-dom'
import { Suspense, lazy } from 'react';
import AuthRoute from './AuthRoute';
import Loading from './Loading';

{noLazyImport}

{lazyImport}

export const {name}=[
{routes}
]


export default {name};
`

export const routes_js_template = `
{noLazyImport}


export const {name}=[
{routes}
]


export default {name};
`


